﻿package morn.core.components
{
    import flash.events.*;
    import morn.core.events.*;
    import morn.core.utils.*;

    public class TextArea extends TextInput implements ISkinAbleContainer
    {
        protected var _scrollBar:VScrollBar;
        protected var _lineHeight:Number;
        private var _skinChanged:Boolean = false;

        public function TextArea(param1:String = null)
        {
            super(param1);
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                if ((param1 as String).indexOf("scroll") > -1)
                {
                    this.scrollBarSkin = param1 as String;
                }
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            var _loc_1:* = new VScrollBar();
            this._scrollBar = new VScrollBar();
            addChild(_loc_1);
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            width = 180;
            height = 150;
            _textField.wordWrap = true;
            _textField.multiline = true;
            _textField.addEventListener(Event.SCROLL, this.onTextFieldScroll);
            this._scrollBar.addEventListener(Event.CHANGE, this.onScrollBarChange);
            return;
        }// end function

        override protected function changeSize() : void
        {
            _textField.width = _width - _margin[0] - _margin[2];
            _textField.height = _height - _margin[1] - _margin[3];
            if (Boolean(this._scrollBar.skin))
            {
                _textField.width = _textField.width - this._scrollBar.width - 2;
                this._scrollBar.height = _height - _margin[1] - _margin[3];
                this._scrollBar.x = _width - this._scrollBar.width - _margin[2];
                this._scrollBar.y = _margin[1];
                App.timer.doFrameOnce(1, this.onTextFieldScroll, [null]);
            }
            return;
        }// end function

        override protected function changeText() : void
        {
            super.changeText();
            this._lineHeight = ObjectUtils.getTextField(_format).textHeight;
            return;
        }// end function

        protected function onScrollBarChange(event:Event) : void
        {
            var _loc_2:* = this._scrollBar.value / this._lineHeight;
            if (_textField.scrollV != _loc_2)
            {
                _textField.removeEventListener(Event.SCROLL, this.onTextFieldScroll);
                _textField.scrollV = _loc_2;
                _textField.addEventListener(Event.SCROLL, this.onTextFieldScroll);
                sendEvent(UIEvent.SCROLL);
            }
            return;
        }// end function

        protected function onTextFieldScroll(event:Event) : void
        {
            if (Boolean(this._scrollBar.skin))
            {
                if (_textField.maxScrollV < 2)
                {
                    this._scrollBar.visible = false;
                }
                else
                {
                    this._scrollBar.visible = true;
                    this._scrollBar.target = this;
                    this._scrollBar.thumbPercent = (_textField.numLines - _textField.maxScrollV + 1) / _textField.numLines;
                    this._scrollBar.scrollSize = this._lineHeight;
                    this._scrollBar.setScroll(this._lineHeight, _textField.maxScrollV * this._lineHeight, _textField.scrollV * this._lineHeight);
                }
            }
            sendEvent(UIEvent.SCROLL);
            return;
        }// end function

        public function get scrollBarSkin() : String
        {
            return this._scrollBar.skin;
        }// end function

        public function set scrollBarSkin(param1:String) : void
        {
            this._scrollBar.skin = param1;
            callLater(this.changeSize);
            this._skinChanged = true;
            return;
        }// end function

        public function get scrollBar() : VScrollBar
        {
            return this._scrollBar;
        }// end function

        public function get maxScrollV() : int
        {
            return _textField.maxScrollV;
        }// end function

        public function scrollTo(param1:int) : void
        {
            commitMeasure();
            _textField.scrollV = param1;
            return;
        }// end function

    }
}
